package nl.samenloop.bigscreen.panels;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JPanel;

import nl.samenloop.bigscreen.Constants;
import nl.samenloop.bigscreen.component.TransparentLabel;
import nl.samenloop.bigscreen.component.TransparentPanel;
import nl.samenloop.bigscreen.fx.FadeIn;
import nl.samenloop.bigscreen.fx.FadeOut;
import nl.samenloop.bigscreen.fx.FxListener;

@SuppressWarnings("serial")
public class CenterPanel extends JPanel {

	//private JPanel titlePanel;
	private JLabel titleField;
	private TransparentPanel contentPanel;
	
	private int titleFade;

	public CenterPanel(int width, int height) {
		setLayout(null);
		setSize(width, height);

		setTitleFade();
	
		setBackground(Constants.PURPLE);

		JPanel title = createCenterTitle(width);
		title.setBounds(0, 0, title.getWidth(), title.getHeight());

		contentPanel = new TransparentPanel();
		contentPanel.setLayout(null);
		contentPanel.setBackground(Constants.PURPLE);
		contentPanel.setBounds(0,title.getHeight(), getWidth(), getHeight()-title.getHeight());
		
		add(title);
		add(contentPanel);
		
	}
	
	private void setTitleFade(){
		try {
		titleFade = Integer.parseInt(Constants.getProperty(Constants.TITLE_FADE));
		} catch (NumberFormatException nfde) {
			titleFade = 1500;
		}
	}

	private JPanel createCenterTitle(int width) {
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(null);

		titlePanel.setSize(width, 50);

		titlePanel.setBackground(Color.black);

		titleField = new TransparentLabel();
		// FIXME calculate font size
		titleField.setFont(new Font("Droid Sans", Font.BOLD, 35));
		// titleField.setBounds(20,10, 200,);

		titleField.setForeground(Color.WHITE);

		titlePanel.add(titleField);

		return titlePanel;
	}

	public void setTitle(final String title){
		setTitle(title, null);
	}
	
	public void setTitle(final String title, final FxListener listener) {
		final String current = titleField.getText();

		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				if (current != null && current.length() > 0) {
					FadeOut.start(titleField, titleFade);
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				titleField.setText(title);

				FadeIn.start(titleField, titleFade, listener);

				titleField.setBounds(20, 5, titleField.getPreferredSize().width, titleField.getPreferredSize().height);

			}
		});

		t.start();

	}


	public JPanel getContentPane(){
		return contentPanel;
	}
	

}
